package com.tanhua.server.service;


import com.alibaba.dubbo.config.annotation.Reference;
import com.tanhua.dubbo.server.api.CommentApi;
import com.tanhua.dubbo.server.api.VideoApi;
import com.tanhua.dubbo.server.pojo.Video;
import com.tanhua.server.vo.PageResult;
import com.tanhua.server.vo.VideoVo;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class VideoService {

    @Reference(version = "1.0.0")
    private VideoApi videoApi;

    @Reference(version = "1.0.0")
    private CommentApi commentApi;


    /**
     * 视频记录
     *
     * @param page
     * @param pagesize
     * @param uid
     * @param sortProp
     * @param sortOrder
     * @return
     */
    public PageResult queryVideos(Integer page, Integer pagesize, Integer uid, String sortProp, String sortOrder) {

        PageResult pageResult = new PageResult();
        pageResult.setPage(page);
        pageResult.setPagesize(pagesize);
        //获取用户基本信息 todo

        //获取该用户所发布的所有视频
        Map<String, Object> map = videoApi.queryVideoList(uid, page, pagesize);
        //总条数
        pageResult.setCounts((int)map.get("count"));
        //总页数
        Integer count = Integer.valueOf((int) map.get("count"));
        int pages = (int) Math.ceil(count.doubleValue() / pagesize.doubleValue());
        pageResult.setPages(pages);
        List<Video> videoList = (List<Video>) map.get("videoList");
        List<VideoVo> videoVoList = new ArrayList<>();

        for (Video video : videoList) {
            VideoVo videoVo = new VideoVo();
            long created = video.getCreated();
            String picUrl = video.getPicUrl();
            String videoUrl = video.getVideoUrl();
            Integer likeCount = commentApi.queryCount(video.getId(), 1);
            Integer commentCount = commentApi.queryCount(video.getId(), 2);

            int userId = video.getUserId().intValue();
            //填充数据
            videoVo.setId(Integer.valueOf(userId));
            videoVo.setUserId(uid);
            videoVo.setVideoUrl(videoUrl);
            videoVo.setPicUrl(picUrl);
            videoVo.setCreateDate((int) created);
            videoVo.setLikeCount(likeCount);
            videoVo.setCommentCount(commentCount);
            videoVo.setReportCount(RandomUtils.nextInt(0,1000));
            videoVo.setForwardingCount(RandomUtils.nextInt(0,1000));
            //todo
            videoVoList.add(videoVo);
        }

        Collections.sort(videoVoList, new Comparator<VideoVo>() {
            @Override
            public int compare(VideoVo o1, VideoVo o2) {
                //根据发布时间
                if (sortProp.equals("createDate")) {
                    return sortOrder.equals("ascending") ? o1.getCreateDate() - o2.getCreateDate() : o2.getCreateDate() - o1.getCreateDate();
                }
                //根据举报数
                else if (sortProp.equals("reportCount")) {
                    return sortOrder.equals("ascending") ? o1.getReportCount() - o2.getReportCount() : o2.getReportCount() - o1.getReportCount();
                }
                //根据喜欢数
                else if (sortProp.equals("likeCount")) {
                    return sortOrder.equals("ascending") ? o1.getLikeCount() - o2.getLikeCount() : o2.getLikeCount() - o1.getLikeCount();
                }
                //根据评论数
                else if (sortProp.equals("commentCount")) {
                    return sortOrder.equals("ascending") ? o1.getCommentCount() - o2.getCommentCount() : o2.getCommentCount() - o1.getCommentCount();
                }else {
                    //根据转发数
                    return sortOrder.equals("ascending") ? o1.getForwardingCount() - o2.getForwardingCount() : o2.getForwardingCount() - o1.getForwardingCount();
                }
            }
        });
        pageResult.setItems(videoVoList);

        return pageResult;
    }


}
